package frsf.cidisi.exercise.tp2_ia.situationCalculus;

import frsf.cidisi.faia.agent.Agent;
import frsf.cidisi.faia.agent.situationcalculus.SituationCalculusPerception;
import frsf.cidisi.faia.environment.Environment;

public class ComputersMarketPerception extends SituationCalculusPerception {

	public static int WAITING_REQUEST = 1;
    public static int REFUSE_FORECAST_REQUEST = 2;
    public static int AGREE_FORECAST_REQUEST = 3;
    public static int PROPOSE_DEMAND_FORECAST_1 = 4;
    public static int PROPOSE_DEMAND_FORECAST_2 = 5;
    public static int CANCEL_DEMAND_FORECAST = 6;
    public static int INFORM_ISSUES = 7;
    
	private int inform_issues;
	private int cancel_demandforecast;
	private int propose_demandforecast1;
	private int agree_forecastrequestresponse;
	private int refuse_forecastrequestresponse;
	private int propose_demandforecast2;
	private int waiting_request;
	
	private int tiempo;
	private String str;

    public ComputersMarketPerception() {
        super();
    }

    @Override
    public void initPerception(Agent agent, Environment environmentIn) {
        
    	TKComputers environment = (TKComputers) environmentIn;
        EstadoAmbiente environmentState = environment.getEnvironmentState();
        
        int posicionAgente = environmentState.getposicionAgente();
        
        if (environmentState.gettpoTranscurrido()>=168){
        	this.setcancel_demandforecast(CANCEL_DEMAND_FORECAST);
        	this.str = "Cancel Demand Forecast";
        }
        else if (posicionAgente == EstadoAgente.INITIAL){
        	this.setwaiting_request(WAITING_REQUEST);
        	this.str = "Waiting for Request";
        }
        else if (posicionAgente == EstadoAgente.FORECAST_REQUEST_SENT){
        	if (aleatorio(0,1)==0){
        		this.setrefuse_forecastrequestresponse(REFUSE_FORECAST_REQUEST);
        		this.str = "Refuse Forecast Request";
        	}
        	else{
        		this.setagree_forecastrequestresponse(AGREE_FORECAST_REQUEST);
        		this.str = "Agree Forecast Request";
        	}
        }
        else if (posicionAgente == EstadoAgente.INFORM_PLANNED_EVENTS_SENT){
        	this.setpropose_demandforecast1(PROPOSE_DEMAND_FORECAST_1);
        	this.str = "Propose Demand Forecast 1";
        }
        else if (posicionAgente == EstadoAgente.INFORM_POS_FORECAST_SENT){
        	this.setpropose_demandforecast1(PROPOSE_DEMAND_FORECAST_1);
        	this.str = "Propose Demand Forecast 1";
        }
        else if (posicionAgente == EstadoAgente.ACCEPT_PROPOSAL_SENT){
    			this.setpropose_demandforecast2(PROPOSE_DEMAND_FORECAST_2);
       	 		this.setStr("Propose Demand Forecast2");
 	
        }
        else if (posicionAgente == EstadoAgente.UPDATED_DEMAND_FORECAST_SENT){
        		this.setpropose_demandforecast2(ComputersMarketPerception.PROPOSE_DEMAND_FORECAST_2);
       	 		this.setStr("Propose Demand Forecast2");
        }
        else if (posicionAgente == EstadoAgente.SUBPROCESS_3_FINALIZED){
    		this.setinform_issues(ComputersMarketPerception.INFORM_ISSUES);
    		this.setStr("Inform Issues");
        }
    }

    @Override
    public String toString() {
        //StringBuffer perceptionString = new StringBuffer("perception([");
    	String retorno = new String("perception(");
        retorno = retorno + nombreProlog() + ",";
        retorno = retorno + getTiempo() + ")";
    	return retorno;
    }
    
    
    public int getinform_issues(){
        return inform_issues;
     }
     public void setinform_issues(int arg){
        this.inform_issues = arg;
        this.tiempo = aleatorio(1,3);
     }
     public int getcancel_demandforecast(){
        return cancel_demandforecast;
     }
     public void setcancel_demandforecast(int arg){
        this.cancel_demandforecast = arg;
        this.tiempo = -1;
     }
     public int getpropose_demandforecast1(){
        return propose_demandforecast1;
     }
     public void setpropose_demandforecast1(int arg){
        this.propose_demandforecast1 = arg;
        this.tiempo = aleatorio(1,3);
     }
     public int getagree_forecastrequestresponse(){
        return agree_forecastrequestresponse;
     }
     public void setagree_forecastrequestresponse(int arg){
        this.agree_forecastrequestresponse = arg;
        this.tiempo = aleatorio(2,3);
     }
     public int getrefuse_forecastrequestresponse(){
        return refuse_forecastrequestresponse;
     }
     public void setrefuse_forecastrequestresponse(int arg){
        this.refuse_forecastrequestresponse = arg;
        this.tiempo = aleatorio(2,4);
     }
     public int getpropose_demandforecast2(){
        return propose_demandforecast2;
     }
     public void setpropose_demandforecast2(int arg){
        this.propose_demandforecast2 = arg;
        this.tiempo = aleatorio(1,2);
     }
     public int getwaiting_request(){
        return waiting_request;
     }
     public void setwaiting_request(int arg){
        waiting_request = arg;
        tiempo = 0;        
     }
     public int getTiempo() {
		return tiempo;
	 }
     public void setTiempo(int tiempo) {
		this.tiempo = tiempo;
	 }
     
     public String getStr() {
		return str;
	}

	public void setStr(String str) {
		this.str = str;
	}

	/**
      * retorna un entero aleatorio entre dos valores
      * @param a
      * @param b
      * @return
      */
     public static int aleatorio(int a, int b){
    	// Valor entre a y b, ambos incluidos.
		int r = (int) Math.floor(Math.random()*(b-a+1)+a);
		return r;
     }
	
     private String nombreProlog(){
    	 
    	 if(this.inform_issues!=0) return "informIssues";
    	 if(this.cancel_demandforecast!=0) return "cancelDemandForecast";
    	 if(this.propose_demandforecast1!=0) return "proposeDemandForecast1";
    	 if(this.agree_forecastrequestresponse!=0) return "agreeForecastRequestResponse";
    	 if(this.refuse_forecastrequestresponse!=0) return "refuseForecastRequestResponse";
    	 if(this.propose_demandforecast2!=0) return "proposeDemandForecast2";
    	 if(this.waiting_request!=0) return "waitingForRequest";
    	 return null;
     }

}
